// ==UserScript==
// @name           bro3_put_deck_text.user.js
// @namespace      http://ronf-bro3.googlecode.com/
// @include        http://*.3gokushi.jp/card/deck.php*
// @include        http://*.1kibaku.jp/card/deck.php*
// @include        http://*.3gokushi.jp/card/domestic_setting.php*
// @include        http://*.1kibaku.jp/card/domestic_setting.php*
// @include        http://*.3gokushi.jp/facility/castle_send_troop.php*
// @include        http://*.1kibaku.jp/facility/castle_send_troop.php*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// @require        http://flot.googlecode.com/svn/trunk/jquery.flot.js
// @description    ブラウザ三国志デッキ情報テキスト生成
// @version        0.07
// ==/UserScript==
// version date       author
// ver0.01 2011.06.16 ronf 実装テスト
// ver0.02 2011.06.16 ronf 最終ページを入手できないバグを修正
// ver0.03 2011.06.26 ronf リスト表示でも15枚表示でも入手できるように修正、デッキの武将も入手できるように修正
// ver0.04 2011.06.27 ronf カードnoを入手できるように修正(一部入手不可)、利用は各自で♪
// ver0.04.1 2011.07.16 ronf 回復中等のスキルが正常に入手できないのを修正
// ver0.05 2011.08.02 ronf 国を入手できるように修正
// ver0.06 2011.08.15 ronf 使用スキル時間が入手できるように修正
// ver0.07 2011.09.15 ronf 使用スキル時間が入手がきちんと使えるように修正。データ列数が変わってます。

var VERSION_KEY = "000pdt";
var VERSION = "0.07";
var urlpath;
var maxpage;
var putText = "";
var card;

var recoverySkillName = "_DUSN";
var recoverySkillDatas;

// 共通関数
var d = document;
var $d = function(id) { return d.getElementById(id); };
var $x = function(xp,dc) { return d.evaluate(xp, dc||d, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; };
var $e = function(key,dc) { return d.evaluate(key, dc||d, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); };

///////////////////////////////////////////////
//JSONでデータ保存
//
//
(function () {
	var CLASS_TYPE = '___class___';
	var VALUE_TYPE = '___value___';
	var DEPTH = 20;
	var convert = function(value, depth) {
		var result;
		if (value instanceof Array) {
			result = [];
			for (var i = 0, l = value.length; i < l; ++i) {
				if (depth > 0) {
					result[i] = convert(value[i], depth - 1);
				}
			}
		} else if (value instanceof Date) {
			result = {};
			result[CLASS_TYPE] = 'Date';
			result[VALUE_TYPE] = value.getTime();
		} else if (value instanceof Object) {
			result = {};
			for (var i in value) {
				if (depth > 0) {
					result[i] = convert(value[i], depth - 1);
				}
			}
		} else {
			result = value;
		}
		return result;
	};
	var deconvert = function(value, depth) {
		if (depth > 0) {
			if (value instanceof Array) {
				for (var i = 0, l = value.length; i < l; ++i) {
					value[i] = deconvert(value[i], depth - 1);
				}
			} else if (value instanceof Object && CLASS_TYPE in value && VALUE_TYPE in value) {
				if (value[CLASS_TYPE] === 'Date') {
					value = new Date(value[VALUE_TYPE]);
				}
			} else if (value instanceof Object) {
				for (var i in value) {
					value[i] = deconvert(value[i], depth - 1);
				}
			}
		}
		return value;
	};
	RB_setJSONValue = function(name, value) {
		value = convert(value, DEPTH);
		var jsonValue = JSON.stringify(value);
		GM_setValue(name, jsonValue);
	};
	RB_getJSONValue = function(name, defaultValue) {
		var jsonValue = GM_getValue(name);
		if (jsonValue != undefined) {
			var value = JSON.parse(jsonValue);
			return deconvert(value, DEPTH);
		} else {
			return defaultValue;
		}
	};
})();

//----------------//
// メインルーチン //
//----------------//
(function(){
	if (location.pathname == "/card/deck.php") {
		// デッキ画面のみ動作

		//HTML追加
		addHtml();
	} else if (location.pathname == "/card/domestic_setting.php" || location.pathname == "/facility/castle_send_troop.php") {
		recoverySkillDatas = RB_getJSONValue(recoverySkillName, {});
		var busyos = $e('//*[@class="commonTables"]/tbody/tr/td//table');
		for (var j = 0, lj = busyos.snapshotLength; j < lj; ++j) {
			var busyo = busyos.snapshotItem(j);
			var recoverySkills = $e('.//td[@class="recovery"]', busyo);
			if (recoverySkills.snapshotLength > 0) {
				var matches = $x('.//td[@class="illust"]/a/@href', busyo).textContent.match(/cardWindow_(\d+)/);
				var id = matches[1];
				recoverySkillDatas[id] = {};
				if (recoverySkills != null) {
					for (var i = 0, li = recoverySkills.snapshotLength; i < li; ++i) {
						var recoverySkill = recoverySkills.snapshotItem(i);
						var skill = recoverySkill.textContent.match(/(.*)LV/i);
						var timeParts = $x('./span', recoverySkill).textContent.match(/(\d{1,3}):(\d{1,2}):(\d{1,2})/i);
						var recoveryTime = new Date();
						recoveryTime.setSeconds(recoveryTime.getSeconds() + parseInt(timeParts[3], 10));
						recoveryTime.setMinutes(recoveryTime.getMinutes() + parseInt(timeParts[2], 10));
						recoveryTime.setHours(recoveryTime.getHours() + parseInt(timeParts[1], 10));
						recoverySkillDatas[id][skill[1]] = {skill: skill[1], recovery: recoveryTime};
					}
				}
			}
			unsafeWindow.console.log(recoverySkillDatas[id]);
		}
		RB_setJSONValue(recoverySkillName, recoverySkillDatas);
	}
})();

//------------//
// HTMLの生成 //
//------------//
function addHtml() {

	//--------------------------------------//
	// 画面生成                             //
	//--------------------------------------//

	//----------------//
	// コンテナの取得 //
	//----------------//
	var container;
	var pager = $e('//*[@class="rotateInfo bottom clearfix"]');
	var bbs = $e('//*[@id="gray02Wrapper"]');
	if (pager.snapshotLength == 0) {
		container = bbs.snapshotItem(0);
	} else {
		container = pager.snapshotItem(0);
	}
	

	//----------------//
	//-- ヘッダー部 --//
	//----------------//
	//-- コントロール配置DIV --//
	var formE = d.createElement("form");
	container.appendChild(formE);
	var linksDiv = d.createElement("div");
	formE.appendChild(linksDiv);

	//-- ツール名称ラベル --//
	var textLabel = d.createElement("span");
	textLabel.id = "toolLabel";
	textLabel.style.fontSize = "16px";
	textLabel.innerHTML = "<br><b>デッキ情報抽出 Ver." + VERSION + "</b>";
	textLabel.style.color = "black";
	textLabel.style.width = "725px";
	linksDiv.appendChild(textLabel);

	//-- (改行) --//
	textLabel = d.createElement("span");
	textLabel.innerHTML = "<br>";
	linksDiv.appendChild(textLabel);

	//-- ページ数表示 --//
	var ckLabel = d.createElement("span");
	ckLabel.style.fontSize = "14px";
	ckLabel.style.marginLeft = "16px";
	ckLabel.innerHTML = "デッキページ数";
	linksDiv.appendChild(ckLabel);

	ckLabel = d.createElement("span");
	ckLabel.style.fontSize = "14px";
	ckLabel.id = "pageNo";
	ckLabel.style.marginLeft = "4px";
	ckLabel.innerHTML = "0";
	linksDiv.appendChild(ckLabel);

	ckLabel = d.createElement("span");
	ckLabel.style.fontSize = "14px";
	ckLabel.style.marginLeft = "4px";
	ckLabel.innerHTML = "/";
	linksDiv.appendChild(ckLabel);

	var data = $e('//ul[@class="pager"]/li[@class="last"]//a[@title="last page"]');
	if( data.snapshotLength > 0 ){
		var ref = data.snapshotItem(0).getAttribute("href");
		var matches = ref.match(/p=(\d+)/);
		maxpage = matches[1];
		urlpath = ref.replace(/\?p=[\d]+/,"\?p=TARGETPAGENO");
	}
	else{
		var data = $e('//ul[@class="pager"]/li/b');
		if( data.snapshotLength > 0 ){
			maxpage = data.snapshotItem(0).textContent;

			var data = $e('//ul[@class="pager"]/li//a[@title="first page"]');
			var ref = data.snapshotItem(0).getAttribute("href");
			urlpath = ref.replace(/\?p=[\d]+/,"\?p=TARGETPAGENO");
		}
		else{
			maxpage = 1;
		}
	}

	ckLabel = d.createElement("span");
	ckLabel.style.fontSize = "14px";
	ckLabel.id = "lastPageNo";
	ckLabel.style.marginLeft = "4px";
	ckLabel.innerHTML = maxpage;
	linksDiv.appendChild(ckLabel);
	ckLabel = d.createElement("span");
	ckLabel.style.fontSize = "14px";
	ckLabel.style.marginLeft = "4px";
	ckLabel.innerHTML = "&nbsp;";
	linksDiv.appendChild(ckLabel);

	//-- 情報取得ボタン --//
	var button1 = d.createElement("input");
	button1.type = "button";
	button1.id = "ckButton1";
	button1.style.fontSize = "12px";
	button1.style.marginLeft = "16px";
	button1.value = "デッキ情報を抽出";
	button1.addEventListener("click", function() {getButtonClicked()}, true);
	linksDiv.appendChild(button1);

	//-- 結果全選択ボタン --//
	var button2 = d.createElement("input");
	button2.type = "button";
	button2.id = "ckButton2";
	button2.style.fontSize = "12px";
	button2.style.marginLeft = "16px";
	button2.value = "結果全選択";
	button2.addEventListener("click", function() {selectButtonClicked()}, true);
	linksDiv.appendChild(button2);

	//-- (改行) --//
	textLabel = d.createElement("span");
	textLabel.innerHTML = "<br>";
	linksDiv.appendChild(textLabel);

	// （ギャップ埋め）
	ckLabel = d.createElement("pre");
	ckLabel.style.height = "6px";
	linksDiv.appendChild(ckLabel);

	//----------------------------//
	// 検索結果のテキスト表示領域 //
	//----------------------------//
	textArea = d.createElement("div");
	textArea.innerHTML = "";
	textArea.style.fontSize = "14px";
	textArea.style.color = "black";
	textArea.style.backgroundColor = "#FFDEAD";
	textArea.style.width = "725px";
	textArea.style.height = "200px";
	textArea.style.overflow = "auto";
	textArea.style.border = "solid 2px";
	textArea.style.padding = "2px";
	textArea.style.marginTop = "-2px";
	textArea.style.textAlign = "left";
	linksDiv.appendChild(textArea);

	ckLabel = d.createElement("pre");
	ckLabel.id = "resultText";
	ckLabel.innerHTML = "";
	textArea.appendChild(ckLabel);

	//--------------------------//
	// 他ページ取得情報の設定域 //
	//--------------------------//
	var linksDiv2 = d.createElement("html");
	linksDiv2.id = "hiddenData";
	linksDiv2.style.display = "none";
	container.appendChild(linksDiv2);
}


//------------------------//
// 取得ボタン押下処理処理 //
//------------------------//
function getButtonClicked() {
	// ハッシュテーブル初期化
	card = new Array();

	var textBox = $e('//*[@id="resultText"]');
	textBox.snapshotItem(0).innerHTML = "データ取得中・・・「終了しました」のダイアログが出るまでお待ち下さい";

	var button = $e('//*[@id="ckButton1"]');
	button.snapshotItem(0).disabled = true;
	button = $e('//*[@id="ckButton2"]');
	button.snapshotItem(0).disabled = true;

	// 見出し
	putText = "ID\tno\tレア\t名前\tコスト\t国\t兵科\tレベル\tExp\tスコア\t攻撃\t知力\t歩防\t槍防\t弓防\t騎防\t速度\tスキル1名\tスキル1LV\tスキル1回復時間\tスキル2名\tスキル2LV\tスキル2回復時間\tスキル3名\tスキル3LV\tスキル3回復時間<br>";
	recoverySkillDatas = RB_getJSONValue(recoverySkillName, {});
	var deckBase = $e('//div[@id="cardListDeck"]//div[@class="cardColmn"]');
	for( var i = 0, li = deckBase.snapshotLength; i < li; ++i ){
		var data1 = $e('.//div[@class="cardWrapper"]//span', deckBase.snapshotItem(i));
		var data2 = $e('.//div[@class="card_back"]/span', deckBase.snapshotItem(i));
		putText += data1.snapshotItem(5).textContent + "\t";
		var noData = "", target, matches;
		target = $e('.//div[@class="illustMini"]/a/@href', deckBase.snapshotItem(i));
		if (target.snapshotLength > 0) {
			var matches = target.snapshotItem(0).textContent.match(/#TB_inline\?height=340&width=500&inlineId=cardWindow_(\d+)$/);
			if (matches) {
				noData = matches[1];
			}
		}
		if (noData == '') {
			target = $e('.//a[@class="set-protection-button"]/@onClick', deckBase.snapshotItem(i));
			if (target.snapshotLength > 0) {
				var matches = target.snapshotItem(0).textContent.match(/^return operationExecution\('[^']+', (\d+), 'protect'\)/);
				if (matches) {
					noData = matches[1];
				} else {
					noData = target.snapshotItem(0).textContent;
				}
			}
		}
		if (noData == '') {
			target = $e('.//span[@class="status_levelup"]/a/@href', deckBase.snapshotItem(i));
			if (target.snapshotLength > 0) {
				var matches = target.snapshotItem(0).textContent.match(/status_info.php\?cid=(\d+)&p=\d+/);
				if (matches) {
					noData = matches[1];
				} else {
					noData = target.snapshotItem(0).textContent;
				}
			}
		}
		if (noData == '') {
			target = $e('.//div[@class="control"]/a/img/@onClick', deckBase.snapshotItem(i));
			if (target.snapshotLength > 0) {
				var matches = target.snapshotItem(0).textContent.match(/^return operationExecution\('[^']+', (\d+), 'unset'\)$/);
				if (matches) {
					noData = matches[1];
				} else {
					noData = target.snapshotItem(0).textContent;
				}
			}
		}
		putText += noData + "\t";
		putText += data1.snapshotItem(0).textContent + "\t";
		putText += data1.snapshotItem(2).textContent + "\t";
		putText += data1.snapshotItem(1).textContent + "\t";
		putText += $e('./img/@title', data2.snapshotItem(2)).snapshotItem(0).textContent + "\t";
		putText += $e('./img/@title', data1.snapshotItem(4)).snapshotItem(0).textContent + "\t";
		putText += data1.snapshotItem(3).textContent + "\t";
		var matches = $e('.//div[@class="cardWrapper"]//span[@class="ex"]', deckBase.snapshotItem(i)).snapshotItem(0).textContent.match(/経験値:(\d+)/);
		putText += matches[1] + "\t";
		putText += data2.snapshotItem(3).textContent + "\t";
		putText += data1.snapshotItem(8).textContent + "\t";
		putText += data1.snapshotItem(9).textContent + "\t";
		putText += data1.snapshotItem(10).textContent + "\t";
		putText += data1.snapshotItem(11).textContent + "\t";
		putText += data1.snapshotItem(12).textContent + "\t";
		putText += data1.snapshotItem(13).textContent + "\t";
		putText += data1.snapshotItem(14).textContent + "\t";

		var now = new Date();
		if (data2.snapshotLength > 5 && (matches = data2.snapshotItem(5).textContent.match(/^(.*):(.*)LV(.*)$/))) {
			putText += matches[2] + "\t" + matches[3] + "\t";
			if (noData != null && recoverySkillDatas[noData] != null && recoverySkillDatas[noData][matches[2]] != null && recoverySkillDatas[noData][matches[2]].recovery.getTime() > now.getTime()) {
				putText += getDateString(recoverySkillDatas[noData][matches[2]].recovery) + "\t";
			} else if ($x('./span[contains(@class, "notice skill-kaifuku")]', data2.snapshotItem(5))) {
				putText += "回復中\t";
			} else {
				putText += "\t";
			}
		} else {
			putText += "\t\t\t";
		}
		if (data2.snapshotLength > 6 && (matches = data2.snapshotItem(6).textContent.match(/^(.*):(.*)LV(.*)$/))) {
			putText += matches[2] + "\t" + matches[3] + "\t";
			if (noData != null && recoverySkillDatas[noData] != null && recoverySkillDatas[noData][matches[2]] != null && recoverySkillDatas[noData][matches[2]].recovery.getTime() > now.getTime()) {
				putText += getDateString(recoverySkillDatas[noData][matches[2]].recovery) + "\t";
			} else if ($x('./span[contains(@class, "notice skill-kaifuku")]', data2.snapshotItem(6))) {
				putText += "回復中\t";
			} else {
				putText += "\t";
			}
		} else {
			putText += "\t\t\t";
		}
		if (data2.snapshotLength > 7 && (matches = data2.snapshotItem(7).textContent.match(/^(.*):(.*)LV(.*)$/))) {
			putText += matches[2] + "\t" + matches[3] + "\t";
			if (noData != null && recoverySkillDatas[noData] != null && recoverySkillDatas[noData][matches[2]] != null && recoverySkillDatas[noData][matches[2]].recovery.getTime() > now.getTime()) {
				putText += getDateString(recoverySkillDatas[noData][matches[2]].recovery) + "\t";
			} else if ($x('./span[contains(@class, "notice skill-kaifuku")]', data2.snapshotItem(7))) {
				putText += "回復中\t";
			} else {
				putText += "\t";
			}
		} else {
			putText += "\t\t\t";
		}
		putText += "<br />";
	}

	if( maxpage > 1 ){
		loadPage(1);
	}
	else{
		// 2ページ目以降
		var base = $e('(//*[@id="hiddenData"]//div[@id="cardFileList"]//div[@class="cardColmn"]) | (//*[@id="hiddenData"]//div[@class="cardStatusDetail"])');
		for( var i = 0, li = base.snapshotLength; i < li; ++i ){
			var data1 = $e('.//div[@class="cardWrapper" or @class="cardWrapper2col"]//span', base.snapshotItem(i));
			var data2 = $e('.//div[@class="card_back"]/span', base.snapshotItem(i));
			putText += data1.snapshotItem(5).textContent + "\t";
			var noData = "", target, matches;
			target = $e('.//div[@class="illustMini"]/a/@href', base.snapshotItem(i));
			if (target.snapshotLength > 0) {
				var matches = target.snapshotItem(0).textContent.match(/#TB_inline\?height=340&width=500&inlineId=cardWindow_(\d+)$/);
				if (matches) {
					noData = matches[1];
				}
			}
			if (noData == '') {
				target = $e('.//a[@class="set-protection-button"]/@onClick', base.snapshotItem(i));
				if (target.snapshotLength > 0) {
					var matches = target.snapshotItem(0).textContent.match(/^return operationExecution\('[^']+', (\d+), 'protect'\)$/);
					if (matches) {
						noData = matches[1];
					} else {
						noData = target.snapshotItem(0).textContent;
					}
				}
			}
			if (noData == '') {
				target = $e('.//span[@class="status_levelup"]/a/@href', base.snapshotItem(i));
				if (target.snapshotLength > 0) {
					var matches = target.snapshotItem(0).textContent.match(/status_info.php\?cid=(\d+)&p=\d+/);
					if (matches) {
						noData = matches[1];
					} else {
						noData = target.snapshotItem(0).textContent;
					}
				}
			}
			if (noData == '') {
				target = $e('.//div[@class="control"]/a/img/@onClick', base.snapshotItem(i));
				if (target.snapshotLength > 0) {
					var matches = target.snapshotItem(0).textContent.match(/^return operationExecution\('[^']+', (\d+), 'set'\)$/);
					if (matches) {
						noData = matches[1];
					} else {
						noData = target.snapshotItem(0).textContent;
					}
				}
			}
			putText += noData + "\t";
			putText += data1.snapshotItem(0).textContent + "\t";
			putText += data1.snapshotItem(2).textContent + "\t";
			putText += data1.snapshotItem(1).textContent + "\t";
			putText += $e('./img/@title', data2.snapshotItem(2)).snapshotItem(0).textContent + "\t";
			putText += $e('./img/@title', data1.snapshotItem(4)).snapshotItem(0).textContent + "\t";
			putText += data1.snapshotItem(3).textContent + "\t";
			var matches = $e('.//div[@class="cardWrapper" or @class="cardWrapper2col"]//span[@class="ex"]', base.snapshotItem(i)).snapshotItem(0).textContent.match(/経験値:(\d+)/);
			putText += matches[1] + "\t";
			putText += data2.snapshotItem(3).textContent + "\t";
			putText += data1.snapshotItem(8).textContent + "\t";
			putText += data1.snapshotItem(9).textContent + "\t";
			putText += data1.snapshotItem(10).textContent + "\t";
			putText += data1.snapshotItem(11).textContent + "\t";
			putText += data1.snapshotItem(12).textContent + "\t";
			putText += data1.snapshotItem(13).textContent + "\t";
			putText += data1.snapshotItem(14).textContent + "\t";

			var now = new Date();
			if (data2.snapshotLength > 5 && (matches = data2.snapshotItem(5).textContent.match(/^(.*):(.*)LV(.*)$/))) {
				putText += matches[2] + "\t" + matches[3] + "\t";
				if (noData != null && recoverySkillDatas[noData] != null && recoverySkillDatas[noData][matches[2]] != null && recoverySkillDatas[noData][matches[2]].recovery.getTime() > now.getTime()) {
					putText += getDateString(recoverySkillDatas[noData][matches[2]].recovery) + "\t";
				} else if ($x('./span[contains(@class, "notice skill-kaifuku")]', data2.snapshotItem(5))) {
					putText += "回復中\t";
				} else {
					putText += "\t";
				}
			} else {
				putText += "\t\t\t";
			}
			if (data2.snapshotLength > 6 && (matches = data2.snapshotItem(6).textContent.match(/^(.*):(.*)LV(.*)$/))) {
				putText += matches[2] + "\t" + matches[3] + "\t";
				if (noData != null && recoverySkillDatas[noData] != null && recoverySkillDatas[noData][matches[2]] != null && recoverySkillDatas[noData][matches[2]].recovery.getTime() > now.getTime()) {
					putText += getDateString(recoverySkillDatas[noData][matches[2]].recovery) + "\t";
				} else if ($x('./span[contains(@class, "notice skill-kaifuku")]', data2.snapshotItem(6))) {
					putText += "回復中\t";
				} else {
					putText += "\t";
				}
			} else {
				putText += "\t\t\t";
			}
			if (data2.snapshotLength > 7 && (matches = data2.snapshotItem(7).textContent.match(/^(.*):(.*)LV(.*)$/))) {
				putText += matches[2] + "\t" + matches[3] + "\t";
				if (noData != null && recoverySkillDatas[noData] != null && recoverySkillDatas[noData][matches[2]] != null && recoverySkillDatas[noData][matches[2]].recovery.getTime() > now.getTime()) {
					putText += getDateString(recoverySkillDatas[noData][matches[2]].recovery) + "\t";
				} else if ($x('./span[contains(@class, "notice skill-kaifuku")]', data2.snapshotItem(7))) {
					putText += "回復中\t";
				} else {
					putText += "\t";
				}
			} else {
				putText += "\t\t\t";
			}
			putText += "<br />";
		}

		var textBox = $e('//*[@id="resultText"]');
		var output = '';
		textBox.snapshotItem(0).innerHTML = putText;

		var button = $e('//*[@id="ckButton1"]');
		button.snapshotItem(0).disabled = false;
		button = $e('//*[@id="ckButton2"]');
		button.snapshotItem(0).disabled = false;

		alert("終了しました");
	}
}

//----------------------//
// ページのローディング //
//----------------------//
function loadPage(targetPageNo) {
	var opt = {
		method: 'get',
		url: urlpath.replace('TARGETPAGENO', targetPageNo),
		onload: function(res) {
			addResult(targetPageNo, res);
		}
	}
	GM_xmlhttpRequest(opt); 
}

//--------------//
// 取得結果加算 //
//--------------//
function addResult(targetPageNo,res) {
	var pageNo = $e('//*[@id="pageNo"]');
	pageNo.snapshotItem(0).textContent = targetPageNo;

	var hidden = $e('//*[@id="hiddenData"]');
	hidden.snapshotItem(0).innerHTML = res.responseText;

//	putText = "ID\tno\tレア\t名前\tコスト\t国\t兵科\tレベル\tExp\tスコア\t攻撃\t知力\t歩防\t槍防\t弓防\t騎防\t速度\tスキル1名\tスキル1LV\tスキル1回復時間\tスキル2名\tスキル2LV\tスキル2回復時間\tスキル3名\tスキル3LV\tスキル3回復時間<br>";

	// 2ページ目以降

	var base = $e('(//*[@id="hiddenData"]//div[@id="cardFileList"]//div[@class="cardColmn"]) | (//*[@id="hiddenData"]//div[@class="cardStatusDetail"])');
	for( var i = 0, li = base.snapshotLength; i < li; ++i ){
		var data1 = $e('.//div[@class="cardWrapper" or @class="cardWrapper2col"]//span', base.snapshotItem(i));
		var data2 = $e('.//div[@class="card_back"]/span', base.snapshotItem(i));
		putText += data1.snapshotItem(5).textContent + "\t";
		var noData = "", target, matches;
		target = $e('.//div[@class="illustMini"]/a/@href', base.snapshotItem(i));
		if (target.snapshotLength > 0) {
			var matches = target.snapshotItem(0).textContent.match(/#TB_inline\?height=340&width=500&inlineId=cardWindow_(\d+)$/);
			if (matches) {
				noData = matches[1];
			}
		}
		if (noData == '') {
			target = $e('.//a[@class="set-protection-button"]/@onClick', base.snapshotItem(i));
			if (target.snapshotLength > 0) {
				var matches = target.snapshotItem(0).textContent.match(/^return operationExecution\('[^']+', (\d+), 'protect'\)$/);
				if (matches) {
					noData = matches[1];
				} else {
					noData = target.snapshotItem(0).textContent;
				}
			}
		}
		if (noData == '') {
			target = $e('.//span[@class="status_levelup"]/a/@href', base.snapshotItem(i));
			if (target.snapshotLength > 0) {
				var matches = target.snapshotItem(0).textContent.match(/status_info.php\?cid=(\d+)&p=\d+/);
				if (matches) {
					noData = matches[1];
				} else {
					noData = target.snapshotItem(0).textContent;
				}
			}
		}
		if (noData == '') {
			target = $e('.//div[@class="control"]/a/img/@onClick', base.snapshotItem(i));
			if (target.snapshotLength > 0) {
				var matches = target.snapshotItem(0).textContent.match(/^return operationExecution\('[^']+', (\d+), 'set'\)$/);
				if (matches) {
					noData = matches[1];
				} else {
					noData = target.snapshotItem(0).textContent;
				}
			}
		}
		putText += noData + "\t";
		putText += data1.snapshotItem(0).textContent + "\t";
		putText += data1.snapshotItem(2).textContent + "\t";
		putText += data1.snapshotItem(1).textContent + "\t";
		putText += $e('./img/@title', data2.snapshotItem(2)).snapshotItem(0).textContent + "\t";
		putText += $e('./img/@title', data1.snapshotItem(4)).snapshotItem(0).textContent + "\t";
		putText += data1.snapshotItem(3).textContent + "\t";
		var matches = $e('.//div[@class="cardWrapper" or @class="cardWrapper2col"]//span[@class="ex"]', base.snapshotItem(i)).snapshotItem(0).textContent.match(/経験値:(\d+)/);
		putText += matches[1] + "\t";
		putText += data2.snapshotItem(3).textContent + "\t";
		putText += data1.snapshotItem(8).textContent + "\t";
		putText += data1.snapshotItem(9).textContent + "\t";
		putText += data1.snapshotItem(10).textContent + "\t";
		putText += data1.snapshotItem(11).textContent + "\t";
		putText += data1.snapshotItem(12).textContent + "\t";
		putText += data1.snapshotItem(13).textContent + "\t";
		putText += data1.snapshotItem(14).textContent + "\t";

		var now = new Date();
		if (data2.snapshotLength > 5 && (matches = data2.snapshotItem(5).textContent.match(/^(.*):(.*)LV(.*)$/))) {
			putText += matches[2] + "\t" + matches[3] + "\t";
			if (noData != null && recoverySkillDatas[noData] != null && recoverySkillDatas[noData][matches[2]] != null && recoverySkillDatas[noData][matches[2]].recovery.getTime() > now.getTime()) {
				putText += getDateString(recoverySkillDatas[noData][matches[2]].recovery) + "\t";
			} else if ($x('./span[contains(@class, "notice skill-kaifuku")]', data2.snapshotItem(5))) {
				putText += "回復中\t";
			} else {
				putText += "\t";
			}
		} else {
			putText += "\t\t\t";
		}
		if (data2.snapshotLength > 6 && (matches = data2.snapshotItem(6).textContent.match(/^(.*):(.*)LV(.*)$/))) {
			putText += matches[2] + "\t" + matches[3] + "\t";
			if (noData != null && recoverySkillDatas[noData] != null && recoverySkillDatas[noData][matches[2]] != null && recoverySkillDatas[noData][matches[2]].recovery.getTime() > now.getTime()) {
				putText += getDateString(recoverySkillDatas[noData][matches[2]].recovery) + "\t";
			} else if ($x('./span[contains(@class, "notice skill-kaifuku")]', data2.snapshotItem(6))) {
				putText += "回復中\t";
			} else {
				putText += "\t";
			}
		} else {
			putText += "\t\t\t";
		}
		if (data2.snapshotLength > 7 && (matches = data2.snapshotItem(7).textContent.match(/^(.*):(.*)LV(.*)$/))) {
			putText += matches[2] + "\t" + matches[3] + "\t";
			if (noData != null && recoverySkillDatas[noData] != null && recoverySkillDatas[noData][matches[2]] != null && recoverySkillDatas[noData][matches[2]].recovery.getTime() > now.getTime()) {
				putText += getDateString(recoverySkillDatas[noData][matches[2]].recovery) + "\t";
			} else if ($x('./span[contains(@class, "notice skill-kaifuku")]', data2.snapshotItem(7))) {
				putText += "回復中\t";
			} else {
				putText += "\t";
			}
		} else {
			putText += "\t\t\t";
		}
		putText += "<br />";
	}

	if(maxpage > targetPageNo){
		loadPage(parseInt(targetPageNo)+1);
	}
	else{
		var textBox = $e('//*[@id="resultText"]');
		var output = '';
		textBox.snapshotItem(0).innerHTML = putText;


		var button = $e('//*[@id="ckButton1"]');
		button.snapshotItem(0).disabled = false;
		button = $e('//*[@id="ckButton2"]');
		button.snapshotItem(0).disabled = false;

		alert("終了しました");
	}
}

//--------------//
// 全選択ボタン //
//-------------//
function selectButtonClicked() {
	// 構築ルート情報の選択
	var textBox = $x('//*[@id="resultText"]');

	var objs = textBox.firstChild;
	var obje = textBox.lastChild;

	var range = d.createRange();
	range.setStart(objs,0);
	range.setEnd(obje,obje.textContent.length);
	var sel = getSelection();
	sel.removeAllRanges();
	sel.addRange(range);
}


// 小数桁数フォーマッタ
function fm_num(str){
	var temp;
	temp = String(str);

	if( temp.indexOf(".") < 0 ){
		temp = temp + ".00";
	}
	if( temp.indexOf(".") == 1 ){
		temp = "  " + temp;
	}
	if( temp.indexOf(".") == 2 ){
		temp = " " + temp;
	}
	if( temp.length != 6 ){
		temp = temp + "0";
	}

	return temp;
}

// カード枚数から比率を求める
function calc_per(num,base){
	return fm_num(Math.floor(num/base*10000)/100) + "%";
}

// 桁数整形
function formatRightNumber( num, length ){
	var fix = '    ';
	var str;
	var result = '';

	str = num.toString(10);
	if( str.length < length ){
		result = fix.substr(0,length - str.length) + str;
	}
	else{
		result = str;
	}

	return result;
}

function getDateString(date) {
	var month = (date.getMonth() + 1);
	var day = date.getDate();
	var hour = date.getHours();
	var minute = date.getMinutes();
	var second = date.getSeconds();
	return date.getFullYear().toString() + "-" + (month < 10? '0': '') + month.toString() + "-" + (day < 10? '0': '') + day.toString() + " " + (hour < 10? '0': '') + hour.toString() + ":" + (minute < 10? '0': '') + minute.toString() + ":" + (second < 10? '0': '') + second.toString();
}

